/**
 * 鸡蛋美术 ajax加载对象
 * $Author: 靓崑
 * $Id: ajax_loading.js 17217 2015-7-17 18:02:08
*/


$(function(){
   popup_login.init(); //初始化登录弹窗对象方法
   popup_retrieve_password.init(); //初始化找回密码弹窗对象方法
   glair.init();//初始化初始对象
});

/* 全局初始化对象 */
var glair = {
	init: function(){

		if($('.only_index').length){

			ajax_popup('popup_glair');
		}
	},
	cart_popup: function(){
		$.ajax({url:'ajax_loading.php?act=cart_popup&',type:"POST",data:{},dataType:"json",success:function(data){
			if (data.content !='null'&&data.content !='undefined'&&data.content !='') {
		       	$('body').append(data.content);
			}else{
				$('#formCart').submit();
				return false;
			};
		}});
	}
}

/* 登录对象 */
var popup_login = {
	//初始化方法
	init : function(){
		//关闭登录框
		$('body').on("click", "#close_popup_login,#popup_login .wj_pass" , function(){
			$('#mask,#popup_login').remove();
		});
		/*关闭手机绑定框*/
		$('body').on("click",'.mobile_validated #close_bangding,.mobile_validated .txt',function(){
			$('.mobile_validated,.mobile_validated_zhezhao').remove();
		});


	},returnUrl:'',

	//判断是否输入用户名
	is_uname_value: function(obj){
		if($(obj).val() == ''){
			$('#popup_login .username').addClass('error');
		}else{
			$('#popup_login .username').removeClass('error');
		}
	},

	//判断是否输入密码
	is_password_value: function(obj){
		if($(obj).val() == ''){
			$('#popup_login .password').addClass('error');
		}else{
			$('#popup_login .password').removeClass('error');
		}
	},
	//判断是否输入用户名
	is_username: function(){
	    if($('.username').val()==''){
	      $('#username_notice').html('请输入用户名').show();
	      return false;
	    }else{
	      $('#username_notice').html('').hide();
	      return true;
	    }
	},
	//判断是否输入密码
	is_password: function(){
	    if($('.username').val()==''){
	      $('#password_notice').html('请输入密码').show();
	      return false;
	    }else{
	      $('#password_notice').html('').hide();
	      return true;
	    }
	},
	/*判断是否输入绑定手机*/
	is_mobile: function(obj){
	    if($(obj).val()=='' || $(obj).val().length != 11){
	      $('.bangding .password .yzm').show();
	      $('.bangding .password .yzm2').hide();
	    }else{
	      $('.bangding .password .yzm').hide();
	      $('.bangding .password .yzm2').show();
	    }
	},
	/*发送绑定手机的验证码*/
	send_sms: function(){
		var mobile = $('#mobile_num').val();
        $.ajax({url:'alone.php?act=send_sms2&',type:"POST",data:{mobile:mobile},dataType:"json",success:function(data){
        	if (data.error > 0 ) {
	          	$('#mobile_num').val(data.message).css('color','#ab0409').attr('disabled','disabled');
				setTimeout(function(){
					$('#mobile_num').css('color','#000').val('').attr('disabled','');
				},2000);
        	}else{
				var cur_count = 60;
				popup_login.set_remain_time(cur_count--);

				popup_login.inter_val_obj = setInterval(function(){
					popup_login.set_remain_time(cur_count--);
				},1000);
        	};

        }});
	},
	/*验证码等待*/
	set_remain_time: function(cur_count){
	    if(cur_count <= 0){
	      $('.bangding .password .yzm2').attr("disabled", false);
	      $('.bangding .password .yzm2').val('重新发送').removeClass('no_send');
	      clearInterval(popup_login.inter_val_obj);
	    }else{
	      cur_count --;
	      $('.bangding .password .yzm2').val(cur_count+'秒内输入').addClass('no_send');
	      $('.bangding .password .yzm2').attr("disabled", true);
	    }
	},
	/*绑定手机*/
	binding_mobile: function(){
	    var captcha_num = $("#captcha_num").val(); //验证码
    	var mobile_num = $("#mobile_num").val(); //手机号码
	    if($('#mobile_num').val().length != 11){
	      alert("请输入正确的11位数手机号！");
	    }else if($('#captcha_num').val().length != 6){
	      alert("请输入正确的6位数验证码！");
	    }else{
	      $.ajax({url:'user.php?act=bindingMobile&',type:"POST",data:{captcha_num:captcha_num,mobile_num:mobile_num},dataType:"json",success:function(data){
		        if(data.error == 0){
		        	$('.mobile_validated .bangding').remove();
		        	$('.mobile_validated .close').after(data.content);
		        }else{
		            $('#captcha_num').val(data.message).css('color','#ab0409').attr('disabled','disabled');
					setTimeout(function(){
						$('#captcha_num').css('color','#000').val('').attr('disabled','');
					},2000);
		        }
	      }});
	    }
	},

	/*登录页面提交登录*/
	ajax_login : function(){
		if($('#remember').attr("checked") == true){
	      var remember=1;
	    }else{
	      var remember=0;
	    }
	    if (!popup_login.is_username()||!popup_login.is_password()) {
	    	return false;
	    };

		url="user.php?r="+Math.random();
    	htmlobj=$.post(url, { 'username' : $('.username').val(), 'password' : $('.password').val() , 'remember' : remember , 'act' : 'signin', 'captcha':$('.captcha').val()}, function(data){
        if(data.error == 0){
        	if (data.content) {
        		$('body').append(data.content);
        	}else{
             window.location.href='user.php';
        	};
        }else{
            $('#password_notice').html(data.message).show();
        }
      },'JSON');
    },


	//判断是否输入用户名和密码，是否可以提交登录
	ajax_login_popup: function(){
		if(!$('#popup_login .username').hasClass('error') && !$('#popup_login .password').hasClass('error')){
			var url = "user.php?r="+Math.random();
			var username = $('#popup_login .uname_value').val();
			var password = $('#popup_login .password_value').val();
			var act = "signin";
			var async = true;
			var type = "post";
			var dataType = "json";
			var data = {
				act : act,
				username : username,
				password : password
			};
			$.ajax({url:url,async:async,type:type,dataType:dataType,data:data,success:function(data){
				if(data.error == 0){
					if(popup_login.returnUrl){
						location.href=popup_login.returnUrl;
					}else{
						location.reload();
					}
				}else{
					$('#popup_login .uname_value').val(data.message).css('color','#ab0409').attr('disabled',true);
					setTimeout(function(){
						$('#popup_login .uname_value').css('color','#000').val('').attr('disabled',false);
					},2000);
				}
			}});
		}else{
			$('#popup_login .uname_value').val('用户名和密码不能为空').css('color','#ab0409').attr('disabled',true);
			setTimeout(function(){
				$('#popup_login .uname_value').css('color','#000').val('').attr('disabled',false);
			},2000);
		}
	}
}


/* 找回密码弹窗对象 */
var popup_retrieve_password = {
	// 初始化方法
	init: function(){
		//关闭找回密码框
		$('body').on("click",'#popup_retrieve_password .img,#popup_retrieve_password .popup_login',function(){
			$('#mask,#popup_retrieve_password').remove();
			clearInterval(popup_retrieve_password.inter_val_obj);
		});
		if ($("#login").length <= 0) {
			$('body').on("click",'#popup_retrieve_password .popup_login',function(){
				ajax_popup('login');
			});

		};
	},
	//关闭找回密码框
	close_popup: function(){
		$('#mask,#popup_retrieve_password').remove();
	},

	//判断用户名并提交
	ajax_retrieve_password: function(){
	    var not_null = "用户名不能为空";
	    var username_invalid = "请输入正确的手机号或邮箱";
	    var not_username = "用户名不存在";
	    var regular_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	    var regular_mobile = /^1[3|4|5|8][0-9]\d{8}$/;
	    var username  = $('#popup_retrieve_password .uname_val').val();
	    var url = "user.php?r="+Math.random();
	    var act = "send_reset_captcha";
		var async = true;
		var type = "post";
		var dataType = "json";
		var data = {
			act : act,
			username : username
		};
		if (username.length == 0){
			$('#popup_retrieve_password .uname_val').val(not_null).css('color','#ab0409').attr('disabled','disabled');
			setTimeout(function(){
				$('#popup_retrieve_password .uname_val').val('').css('color','#000').attr('disabled','');
			},2000);
		}else if((!(regular_email.test(username)))&&(!(regular_mobile.test(username)))){
      		$('#popup_retrieve_password .uname_val').val(username_invalid).css('color','#ab0409').attr('disabled','disabled');
      		setTimeout(function(){
        		$('#popup_retrieve_password .uname_val').val('').css('color','#000').attr('disabled','');
      		},2000);
		}else{
			$.ajax({url:url,async:async,type:type,dataType:dataType,data:data,success:function(data){
				if (data.error>0) {
		      		$('#popup_retrieve_password .uname_val').val(data.message).css('color','#ab0409').attr('disabled','disabled');
		      		setTimeout(function(){
		        		$('#popup_retrieve_password .uname_val').val('').css('color','#000').attr('disabled','');
		      		},2000);
				}else{

					$('#popup_retrieve_password .first').remove();
					$('#popup_retrieve_password .prp_top').after(data.content);
					var cur_count = 60;
					popup_retrieve_password.set_remain_time(cur_count--);
					popup_retrieve_password.inter_val_obj = setInterval(function(){
						popup_retrieve_password.set_remain_time(cur_count--);
					},1000);
					
				};
				return false
			}});
		}
	},

	//发送手机验证码
	rp_send_sms: function(username){
		var url = "user.php?act=send_reset_captcha";
		var async = true;
		var type = "post";
		var dataType = "json";
		var data = {
			mobile : username
		};
		$.ajax({url:url,async:async,type:type,dataType:dataType,data:data,success:function(data){
			if(data.error<=0){
				var cur_count = 60;
				popup_retrieve_password.set_remain_time(cur_count--);
				popup_retrieve_password.inter_val_obj = setInterval(function(){
					popup_retrieve_password.set_remain_time(cur_count--);
				},1000);
			}else{
				$('#captcha_number').val("发送失败").css('color','#ab0409').attr('disabled','disabled');
				setTimeout(function(){
					$('#captcha_number').val('').css('color','#000').attr('disabled','');
				},2000);
			}



		}});
	},

	//验证等待时间
	set_remain_time: function(cur_count){
		if(cur_count <= 1){
			$('#popup_retrieve_password .mobile .captcha .btn').val('重新发送').removeClass('no_send').attr("disabled", false);
			clearInterval(popup_retrieve_password.inter_val_obj);
		}else{
			cur_count --;
			$('#popup_retrieve_password .mobile .captcha .btn').val(cur_count+'重新发送').attr("disabled", true).addClass('no_send');
		}
	},

	//找回密码手机验证
	rp_validate_mobile: function(){
		var captcha_number = $('#captcha_number').val(); //获取验证码
	    var success_captch = "请输入正确的验证码";
	    var url = "user.php?act=rp_validate_mobile&";
		var async = true;
		var type = "post";
		var dataType = "json";
		var data = {
			captcha_number : captcha_number
		};
	    if(captcha_number.length != 6){
	    	$('#captcha_number').val(success_captch).css('color','#ab0409').attr('disabled','disabled');
	    	setTimeout(function(){
	      		$('#captcha_number').val('').css('color','#000').attr('disabled','');
	    	},2000);
	    }else{
	    	$.ajax({url:url,async:async,type:type,dataType:dataType,data:data,success:function(data){


	    		if (data.error>0) {
		          	$('#captcha_number').val(success_captch).css('color','#ab0409').attr('disabled','disabled');
		      		setTimeout(function(){
		      			$('#captcha_number').val('').css('color','#000').attr('disabled','');
		      		},2000);
	    		}else{
					$('#popup_retrieve_password .mobile').remove();
					$('#popup_retrieve_password .prp_top').after(data.content);
	    		};

	    		return false
		        if(result.success){
		        	$('#popup_retrieve_password .mobile').hide();
		         	$('#popup_retrieve_password .confirm').show();
		         	$('#popup_retrieve_password .title .text').html('重设密码');
		        }else{
		          	$('#captcha_number').val(success_captch).css('color','#ab0409').attr('disabled','disabled');
		      		setTimeout(function(){
		      			$('#captcha_number').val('').css('color','#000').attr('disabled','');
		      		},2000);
		        }
	      	}});
	    }
	},

	//重设密码提交
	confirm_password: function(){
		var password = $('#popup_retrieve_password .confirm .password_val').val();
		var confirm_password = $('#popup_retrieve_password .confirm .confirm_password_val').val();
		var captcha =$('#popup_retrieve_password .confirm #captcha').val();
		var password_null = "密码不能为空";
		var password_error = "密码由大于6位数字母、数字或下划线组成";
		var unlikeliness = "两次密码不一致";
		var confirm_error = "重置失败";
		var url = "user.php?act=ajax_change_password&";
		var async = true;
		var type = "post";
		var dataType = "json";
		var data = {
			captcha  : captcha,
			password : password
		};
		if(password.length == 0 || confirm_password.length == 0){
			$('#popup_retrieve_password .confirm .password_val').val(password_null).css('color','#ab0409').attr({disabled:'disabled',Type:'text'});
			setTimeout(function(){
				$('#popup_retrieve_password .confirm .password_val').val('').css('color','#000').attr({disabled:'',Type:'password'});
			},2000);
		}
		else if(password.match(/\W/) || password.length < 6){
			$('#popup_retrieve_password .confirm .password_val').val(password_error).css('color','#ab0409').attr({disabled:'disabled',Type:'text'});
			setTimeout(function(){
				$('#popup_retrieve_password .confirm .password_val').val('').css('color','#000').attr({disabled:'',Type:'password'});
			},2000);
		}
		else if(confirm_password != password){
			$('#popup_retrieve_password .confirm .password_val').val(unlikeliness).css('color','#ab0409').attr({disabled:'disabled',Type:'text'});
			setTimeout(function(){
				$('#popup_retrieve_password .confirm .password_val').val('').css('color','#000').attr({disabled:'',Type:'password'});
			},2000);
		}
		else{
			$.ajax({url:url,async:async,type:type,dataType:dataType,data:data,success:function(data){
				if (data.error>0) {
		          	$('#popup_retrieve_password .confirm .password_val').val(data.message).css('color','#ab0409').attr({disabled:'disabled',Type:'text'});
      				setTimeout(function(){
      					$('#popup_retrieve_password .confirm .password_val').val('').css('color','#000').attr({disabled:'',Type:'password'});
      				},2000);
				}else{
					$('#popup_retrieve_password .prp_top,#popup_retrieve_password .confirm').remove();
					$('#popup_retrieve_password .close').after(data.content);
				};
				return false;

			}});
		}
	}

}




/* 倒计时对象 */
/*var timer = {
  total : 60,
  timeSs:null,
  init : function(){
    if(timer.timeSs){
      clearInterval(timer.timeSs);
    }
    timer.timeSs = setInterval("timer.setTime()",1000);
  },
  setTime : function(){
    if(timer.total<=58){
      document.write("over");
      clearInterval(timer.timeSs);
    }else{
      document.write(timer.total--+"<br/>");
    }
  }
}
timer.init();*/
